<?php

/**
 * TimelineController
 * 
 * @author
 * @version 
 */
require_once 'Events.php';
require_once 'Status.php';
require_once 'forms/EventForm.php';
require_once 'Zend/Date.php';
require_once 'Zend/Dojo.php';
require_once 'Zend/Controller/Action.php';

/**
 * Add / Remove actions from timeline events
 *
 */
class TimelineController extends Zend_Controller_Action {
	
	/**
	 * @var Events
	 */
	private $events;
	
	/**
	 * Enter description here...
	 *
	 * @return EventForm
	 */
 	public function getForm()    {        
		$form = new EventForm();
 		return $form;    
 	}
	
	public function init() {
		$this->events = new Events ( );
		$this->view->addHelperPath ( '../application/default/views/helpers', 'Zend_View_Helper' );
		Zend_Dojo::enableView ( $this->view );
	}
	
	/**
	 * The default action - show the home page
	 */
	public function addAction() {
		if (!$this->getRequest()->isPost()) {            
			$this->view->form = $this->getForm();
			return;
		}
		
		$form = $this->getForm();        
		if (!$form->isValid($_POST)) {            
			// Failed validation; redisplay form            
			$this->view->form = $form;
    		return;
		}                
		$values = $form->getValues();        
		$row = $this->events->createRow($values);
		$id = $row->save();
		
		if ($this->getRequest()->isXmlHttpRequest()) {
			$this->_helper->json(array($id));
			return;	
		}
		
		$this->view->form = $form;
	}
	
	/**
	 * The data action - gets the information to the table
	 */
	public function deleteAction() {
		if ($this->getRequest()->isPost() && $this->getRequest()->getParam("id") > 0) {
			$id = $this->getRequest()->getParam("id");
			$row = $this->events->fetchRow('id = ' . $id);
			$row->delete();
			die();
		} else {
			echo "delete action: no id was selected ";
			die();
		}
		
	}
	
	public function statusAction() {
		
		$st = new Status();
		
		$data = new Zend_Dojo_Data('id', $st->statuses());
	
		$this->_helper->json($data->toArray());
	}
	
}
?>

